﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Publicuse.Entity.AttributeUtil.Validate
{
    /// <summary>
    /// 不等于什么才根据正则表达式验证,如果包合至字符，怎表示验证当前值到至今区间数值
    /// </summary>
    public class NotetovAttribute : BaseValidateAttribute
    {
        private string notequalto = null;
        private string remark = null;
        private string format = null;

        public string GetNotequalto { get { return this.notequalto; } }

        public string GetRemark { get { return this.remark; } }


        public NotetovAttribute(string notequalto, string format,string remark)
        {
            this.format = format;
            this.notequalto = notequalto;
            this.remark = remark;
        }

        public override bool Validate(object oValue)
        {
            if (oValue != null && !string.IsNullOrEmpty(oValue.ToString()))
            {
                if (string.Compare(oValue.ToString(), this.notequalto, true) == 0)
                {
                    return true;
                }
                else
                {
                    var ispass = true;
                    if (this.format.Contains("至"))
                    {
                        var min = Convert.ToInt32(this.format.Split('至')[0]);
                        if(Convert.ToInt32(oValue.ToString())<min || Convert.ToInt32(oValue.ToString()) > DateTime.Now.Year)
                        {
                            ispass = false;
                        }
                    }
                    else
                    {
                        var formats = this.format.Split('=');
                        foreach (var item in formats)
                        {
                            if (oValue != null && !string.IsNullOrEmpty(oValue.ToString()))
                            {
                                if (!Regex.IsMatch(oValue.ToString(), @"(" + item + ")"))
                                {
                                    ispass = false;
                                    break;
                                }
                            }

                        }
                    }
                    
                    return ispass;
                }
            }
            else
            {
                return true;
            }
        }
    }
}
